package com.bt.lms.network.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bt.lms.bo.EnrollmentList;
import com.bt.lms.constant.BOConstant;
import com.bt.lms.constant.EnpBizTypeConstant;
import com.bt.lms.entity.EnrollmentPlan;
import com.bt.lms.entity.page.Page;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.service.EnrollmentPlanService;
import com.bt.lms.service.impl.EnrollmentPlanServiceImpl;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 招生计划模块处理器接口实现
 */
public class EnpModuleHandler implements ModuleHandler {

    @Override
    public void handlerMsg(ReqMsg reqMsg, Socket socket) {
        //1。获取类型
        Short bizType = reqMsg.getBizType();
        //2。处理参数
        String data = reqMsg.getData();
        //调用业务接口
        Object object = bizManage(bizType, data);
        //获取请求类型，是否需要响应
        Byte twoWay = reqMsg.getTwoWay();
        if (twoWay == ReqMsg.NETWORK_TWO_WAY_CHAT) {
            //响应
            RespMsg respMsg = object2ReqMsg(reqMsg, object);
            //调方法，返回一个总内容的byte数组
            byte[] bytes = respMsg.toBytes();
            try {
                OutputStream out = socket.getOutputStream();
                out.write(bytes);
                out.flush();

                //关闭资源
                out.close();
                socket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 响应对象转响应消息
     *
     * @param reqMsg 请求消息
     * @param object 请求对象
     * @return 响应消息
     */
    private RespMsg object2ReqMsg(ReqMsg reqMsg, Object object) {

        //要返回的消息
        RespMsg respMsg = new RespMsg();

        //组装响应消息
        //魔数
        String magicNo = reqMsg.getMagicNo();
        respMsg.setMagicNo(magicNo);
        //协议版本号
        Byte version = reqMsg.getVersion();
        respMsg.setVersion(version);
        //序列化算法
        Byte serialCou = reqMsg.getSerialCou();
        respMsg.setSerialCou(serialCou);
        //事件类型 evenType
        Byte evenType = reqMsg.getEvenType();
        respMsg.setEvenType(evenType);
        //业务类型 bizType
        Short bizTypeBytes = reqMsg.getBizType();
        respMsg.setBizType(bizTypeBytes);
        //请求id 毫秒数 reqId
        Long reqId = reqMsg.getReqId();
        respMsg.setReqId(reqId);
        //保留字段 remainField
        byte[] remainField = reqMsg.getRemainField();
        respMsg.setRemainField(remainField);
        //状态  status
        respMsg.setStatus(RespMsg.NETWORK_STATUS_NORMAL);
        //数据 data
        String data = JSON.toJSONString(object);
        respMsg.setData(data);

        return respMsg;
    }


    /**
     * 适配业务
     *
     * @param bizType 业务类型
     * @param data    数据
     * @return 响应的对象
     */
    private Object bizManage(short bizType, String data) {

        //要返回的对象
        Object object = null;
        //业务实现类
        EnrollmentPlanService enrollmentPlanService = new EnrollmentPlanServiceImpl();
        //适配
        if (bizType == EnpBizTypeConstant.ENP_PLAN_ADD) {
            //将消息内容转换为对象
            EnrollmentPlan enrollmentPlan = JSONObject.parseObject(data, EnrollmentPlan.class);
            //调用添加业务实现类方法
            try {
                object = enrollmentPlanService.add(enrollmentPlan);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        if (bizType == EnpBizTypeConstant.ENP_PLAN_EDIT) {
            //将消息内容转换为对象
            EnrollmentPlan enrollmentPlan = JSONObject.parseObject(data, EnrollmentPlan.class);
            Long id = enrollmentPlan.getId();
            //调用编辑业务实现类方法
            try {
                object = enrollmentPlanService.edit(id, enrollmentPlan);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        if (bizType == EnpBizTypeConstant.ENP_PLAN_DETAIL) {
            //将消息内容转换为对象
            EnrollmentPlan enrollmentPlan = JSONObject.parseObject(data, EnrollmentPlan.class);
            Long id = enrollmentPlan.getId();
            Long teachPlanId = enrollmentPlan.getTeachPlanId();
            //调用查看详情业务实现类方法
            try {
                object = enrollmentPlanService.detail(id, teachPlanId);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        if (bizType == EnpBizTypeConstant.ENP_PLAN_DELETED) {
            //将消息内容转换为对象
            EnrollmentPlan enrollmentPlan = JSONObject.parseObject(data, EnrollmentPlan.class);
            Long id = enrollmentPlan.getId();
            //调用删除业务实现类方法
            try {
                object = enrollmentPlanService.delete(id);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        if (bizType == EnpBizTypeConstant.ENP_PLAN_PAGE_CONDITION_QUERY) {

            Page<EnrollmentList> objectPage = new Page<>();
            Page pageEnp = JSONObject.parseObject(data, Page.class);
            //调用分页条件查询业务实现类方法
            try {
                object = enrollmentPlanService.pagingConditionQuery(objectPage,pageEnp);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }


        return object;
    }
}
